/**
 * problem_041.cpp
 * Copyright (C) 2011 - xrose
 */

#include <iostream>
#include <math.h>
using namespace std;
int isDifdigit(int abc)
{
	int chek[9] = {0};
	int c;
	int cnt=0;
	int nod = 0;
	//cout<<"isDifdigit_abc = "<<abc<<endl;
	while(abc>0)
	{
		c = abc%10;
		if((c!=0)&&(chek[c-1]==0))
		{
			chek[c-1]=1;
			abc = abc/10;
		}else
		{
			return 0;
		}
		cnt++;
	}
	for(int i=0; i<10; i++)
	{
	    if(chek[i]==1)
	    {
	        nod++;
	    } else
	    {
	        break;
	    }
	}
	if(nod == cnt)
	{
	    return 1;
	} else
	{
	    return 0;
	}
}

int isPrime(int numb)
{
    int sqrtnumb = floor(sqrt(numb));
    int i;
    if(numb < 2)
    {
        return 0;
    } else
        if(numb == 2) return 1;
    else
        if(numb%2 == 0) return 0;
    for(i = 3; i<=sqrtnumb; i=i+2)
    {
        if(numb%i == 0)
        {
            return 0;
        }
    }
    return 1;
}

int main (int argc, char const* argv[])
{
    //noted that: every nine and eight pandigital alway divisible by 3 (1+2+3+4+5+6+7+8+9 = 45 divisible by 3)
	for(int i = 7654321;i>2;i-=2)
	{
//	    cout<<i<<endl;
	    if(isDifdigit(i))
	    {
	    	if(isPrime(i))
			{
				cout<<"PRIME: "<<i<<endl;
				break;
			}
	    }
	}
	return 0;
}
